package models;

import java.util.List;

import javax.persistence.Entity;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ObjectNode;

import jiyici.Constant.ModelColumn;
import play.db.ebean.Model;

@Entity
public class Record extends ServerBaseModel {

    private static final long serialVersionUID = -3741852635446997804L;

//    @ManyToOne
//    public Item item;
    public long itemId;

    public long recordTime;
    
    public static Model.Finder<Long, Record> find = new Model.Finder<Long, Record>(Long.class, Record.class);

//    @OneToMany(cascade = { CascadeType.REFRESH, CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE }, mappedBy = "record")
//    public List<RecordParam> params = new ArrayList<RecordParam>();
    
    public static Record addRecord(long itemId, long recordTime, RecordParam[] params) {
        Record record = new Record();
        record.itemId = itemId;
        record.recordTime = recordTime;
//        int size = params.length;
//        for (int i = 0; i < size; i++) {
//            record.params.add(params[i]);
//        }
        record.save();
        return record;
    }
    
    public static List<Record> modifiedAfter(User user, long time) {
        return find.where().eq("modelId.userId", user.id).gt(ModelColumn.BASE_COLUMN_LASTMODIFIED, time)
                .findList();
    }
    
    @Override
    public ObjectNode toJson() {
        ObjectNode json = super.toJson();
        json.put(ModelColumn.RECORD_ITEM_ID, itemId);
        json.put(ModelColumn.RECORD_RECORD_TIME, recordTime);
        return json;
    }
    
    @Override
    public void fromJson(JsonNode json) {
        super.fromJson(json);
        itemId = json.findPath(ModelColumn.RECORD_ITEM_ID).getLongValue();
        recordTime = json.findPath(ModelColumn.RECORD_RECORD_TIME).getLongValue();
    }

}
